﻿using System;
using System.IO;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.Teacher.internals.publics.courseware
{
    public partial class D : TeacherBasePage
    {
        protected override void ShowPage()
        {
            if (ispost)
            {
                string request_id = XRequest.GetString("arrv");
                if (request_id != "")
                {
                    string[] idarr;
                    if (request_id.IndexOf(',') != -1)
                    {
                        idarr = request_id.Split(',');
                    }
                    else
                        idarr = new string[] { request_id };

                    int rst = 0;
                    int pageErr = 0;
                    
                    CourseBLL bll = new CourseBLL();
                    IList<CourseWareModel> list = bll.GetCourseWareInfoById(idarr);
                    if (list.Count > 0)
                    {
                        foreach (CourseWareModel model in list)
                        {
                            string imgurl = GetSubValue(model.ImgUrl);
                            string soundurl = GetSubValue(model.SoundUrl);
                            string videourl = GetSubValue(model.VideoUrl);
                            string serverPath = System.Web.HttpContext.Current.Server.MapPath("/");
                            int err = 0;

                            if (!imgurl.Equals(""))
                            {
                                try
                                {
                                    File.Delete(serverPath + imgurl);
                                }
                                catch (IOException ioEx)
                                {
                                    err++;
                                }
                            }

                            if (!soundurl.Equals(""))
                            {
                                try
                                {
                                    File.Delete(serverPath + soundurl);
                                }
                                catch (IOException ioEx)
                                {
                                    err++;
                                }
                            }

                            if (!videourl.Equals(""))
                            {
                                try
                                {
                                    File.Delete(serverPath + videourl);
                                }
                                catch (IOException ioEx)
                                {
                                    err++;
                                }
                            }

                            if (err == 0)
                            {
                                rst = bll.DelCourseWare(model.ID);
                                if (rst > 0)
                                {
                                    bll.UpdateSerialNumber(model.SerialNumber, model.CourseID);
                                }
                            }
                            else
                            {
                                pageErr++;
                            }
                        }
                    }

                    ExecuteJS("alert('操作完成，共有" + pageErr + "个错误'); window.location.replace('/internals/publics/courseware/List.aspx');");
                }
            }
        }

        private string GetSubValue(string v)
        {
            if (!v.Equals(""))
            {
                if (v.IndexOf("//") != -1)
                {
                    v = v.Substring(v.IndexOf("//") + 2);
                    v = v.Substring(v.IndexOf("/"));
                }
            }
            return v;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
